var t = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../@babel/runtime/regenerator")), e = require("../../../@babel/runtime/helpers/asyncToGenerator"), a = require("../../../@babel/runtime/helpers/slicedToArray"), s = require("../../../@babel/runtime/helpers/toConsumableArray"), i = require("../../../@babel/runtime/helpers/defineProperty"), o = getApp(), r = require("../../../mybehaviors/location/location");

Component({
    behaviors: [ r ],
    properties: {
        datas: {
            type: null
        },
        fixedTop: {
            type: Boolean,
            value: !1
        },
        bottomShowBol: {
            type: null
        },
        showDropdownMenu: {
            type: !1
        },
        bottomShowBolIndex: {
            type: null
        },
        index: {
            type: null
        },
        btnFlag: {
            type: null
        },
        pageScrollTopV: {
            type: null,
            value: 0
        }
    },
    options: {
        addGlobalClass: !0,
        styleIsolation: "shared"
    },
    data: {
        tabIndex: 0,
        navbarInitTop: 0,
        tabs: [],
        arrIndex: 1,
        language: "",
        perPage: 15,
        couponGoodsList: [],
        list: [],
        isLoadMore: !0,
        allLoaded: !1,
        query: {
            basic_info: 1,
            page: 1,
            longitude: "",
            latitude: "",
            order_by: "distance",
            search: {
                is_recommand: 0,
                province_id: 0,
                city_id: 0,
                district_id: 0,
                street_id: 0,
                is_free: 0,
                is_group: 0,
                is_new: 0,
                category_ids: [],
                category_id: 0
            }
        },
        love: "",
        point: "",
        totalPage: 0,
        cityId: [],
        provinceData: [],
        cityData: [],
        districtData: [],
        addressData: [ {
            text: "全国",
            value: 0
        } ],
        distance: [ {
            text: "距离优先",
            value: "distance"
        }, {
            text: "人气优先",
            value: "visit_count"
        }, {
            text: "上新优先",
            value: "start_time"
        } ],
        lastClassId: 0,
        nextClassId: 0,
        firstId: 0,
        secondId: 0,
        categoryList: [],
        secondArr: [],
        threeArr: [],
        categoryIndex: 0,
        secondIndex: 0,
        threeIndex: 0,
        styleTwoIndex: null,
        styleThreeIndex: null,
        cityValue: 0,
        classifyValue: "全部分类",
        tagBgColor: "#ffffff",
        container: null,
        stickyRefresh: !1
    },
    observers: {
        datas: function(t) {
            this.data.totalPage <= this.data.page && this.setData({
                isLoadMore: !1,
                allLoaded: !0
            });
        },
        bottomShowBol: function(t) {
            t && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore ? this.getMoreData() : this.data.page >= this.data.totalPage && this.data.couponGoodsList.slice(this.data.per_page * this.data.arrIndex).length > 0 && this.setData({
                list: this.data.list.concat(this.data.couponGoodsList.slice(this.data.perPage * this.data.arrIndex, this.data.perPage * (this.data.arrIndex + 1))),
                arrIndex: this.data.arrIndex + 1
            });
        }
    },
    lifetimes: {
        created: function() {},
        attached: function() {
            var t = this, e = wx.getStorageSync("langIndex");
            this.setData({
                language: e.en
            });
            var a = [ "diy_title_1", "diy_title_2", "diy_title_3", "diy_title_4", "diy_title_5" ], s = this.data.tabs, i = this.data.datas;
            for (var o in a) s.push({
                name: i[a[o]],
                status: Number(o) + 1
            });
            var r = s.filter(function(t) {
                return i.tab_list.some(function(e) {
                    return e === String(t.status);
                });
            });
            this.setData({
                tabs: r,
                tagBgColor: i.tab_bg_color
            }), this.isTabActions(this.data.tabs[0].status), this.goodsCategoryList(), this.setData({
                container: function() {
                    return t.createSelectorQuery().select("#container");
                }
            });
        }
    },
    pageLifetimes: {
        show: function() {},
        hide: function() {},
        resize: function(t) {}
    },
    ready: function() {
        var t = this;
        try {
            this._getLocation(function(e, a) {
                var s = [];
                s.push({
                    text: e.ad_info.city,
                    value: e.ad_info.adcode
                }), t.setData({
                    "query.longitude": a.lng,
                    "query.latitude": a.lat,
                    addressData: s,
                    cityValue: Number(e.ad_info.adcode)
                }), t._initAddressInfo();
            });
        } catch (t) {
            console.log(t);
        }
    },
    methods: {
        goodsCategoryList: function() {
            var t = this, e = o.getNetAddresss("plugin.coupon-store.frontend.category.goodsCategoryList");
            o._postNetWork({
                url: e,
                data: {},
                success: function(e) {
                    var a = e.data, s = a.data.list;
                    if (1 == a.result) {
                        var i = [], o = [];
                        s[t.data.categoryIndex].has_many_children && s[t.data.categoryIndex].has_many_children.length > 0 && (i = s[t.data.categoryIndex].has_many_children), 
                        s[t.data.categoryIndex].has_many_children && s[t.data.categoryIndex].has_many_children.length > 0 && (o = i[t.data.secondIndex].has_many_children), 
                        0 === t.data.firstId && t.setData({
                            firstId: s[0].id
                        }), t.setData({
                            categoryList: s,
                            secondArr: i,
                            threeArr: o,
                            catLevel: a.data.cat_level
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        orderCouponList: function() {
            var t = this, e = o.getNetAddresss("plugin.coupon-store.frontend.goods.index"), a = this.data.query;
            o._postNetWork({
                url: e,
                data: a,
                success: function(e) {
                    var a, s, i = e.data, o = i.data.data;
                    1 == i.result ? t.setData({
                        couponGoodsList: o,
                        totalPage: i.data.last_page,
                        love: null === (a = i.basic_info) || void 0 === a ? void 0 : a.love,
                        point: null === (s = i.basic_info) || void 0 === s ? void 0 : s.point
                    }) : wx.showToast({
                        icon: "none",
                        title: i.msg,
                        duration: 1500
                    });
                    return o;
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        categoryAllOn: function() {
            this.setData({
                categoryIndex: -1,
                lastClassId: 0,
                nextClassId: 0,
                secondId: 0,
                classifyValue: "全部推荐"
            }), this.data.query.search.category_id = 0, this.orderCouponList(), this.selectComponent("#item").toggle(!1);
        },
        firstAllOn: function() {
            this.setData({
                styleTwoIndex: -1,
                "query.search.category_id": this.data.firstId,
                classifyValue: "全部推荐"
            }), this.orderCouponList(), this.selectComponent("#item").toggle(!1);
        },
        threeAllOn: function() {
            this.setData({
                styleThreeIndex: -1,
                "query.search.category_id": this.data.secondId,
                classifyValue: "全部推荐"
            }), this.orderCouponList(), this.selectComponent("#item").toggle(!1);
        },
        categoryOn: function(t) {
            var e = t.currentTarget.dataset, a = e.index, s = e.id, i = e.name, o = e.children, r = this.data.query.search.category_id;
            this.getAdjacentInfo(a), this.data.categoryIndex !== a && this.goodsCategoryList(), 
            Array.isArray(o) && 0 === o.length && (r = s, this.selectComponent("#item").toggle(!1), 
            this.setData({
                classifyValue: i,
                "query.search.category_id": r
            }), this.orderCouponList()), this.setData({
                categoryIndex: a,
                secondIndex: 0,
                secondId: 0,
                styleTwoIndex: null,
                firstId: s
            });
        },
        getAdjacentInfo: function(t) {
            var e;
            this.data.categoryList[t - 1] ? this.setData({
                lastClassId: this.data.categoryList[t - 1].id,
                nextClassId: (null === (e = this.data.categoryList[t + 1]) || void 0 === e ? void 0 : e.id) || 0
            }) : this.setData({
                nextClassId: this.data.categoryList[t + 1].id
            });
            0 !== t && -1 !== t || this.setData({
                lastClassId: 0
            });
        },
        secondOn: function(t) {
            var e = t.currentTarget.dataset, a = e.index, s = e.id, i = e.name, o = e.children, r = this.data.query.search.category_id;
            (Array.isArray(o) && 0 === o.length || "2" === this.data.catLevel) && (r = s, this.selectComponent("#item").toggle(!1), 
            this.setData({
                classifyValue: i,
                "query.search.category_id": r
            }), this.orderCouponList()), this.data.secondIndex !== a && this.goodsCategoryList(), 
            this.setData({
                secondIndex: a,
                styleTwoIndex: a,
                secondId: s
            });
        },
        threeOn: function(t) {
            var e = t.currentTarget.dataset, a = e.index, s = e.name, i = e.id;
            this.setData({
                threeIndex: a,
                styleThreeIndex: a,
                classifyValue: s,
                "query.search.category_id": i
            }), this.selectComponent("#item").toggle(!1), this.goodsCategoryList(), this.orderCouponList();
        },
        onTabChange: function(t) {
            var e = this, a = t.detail.index;
            this.onMenuClose(), this.setData({
                stickyRefresh: !0
            }), this.isTabActions(this.data.tabs[a].status), this.setData({
                container: function() {
                    return e.createSelectorQuery().select("#container");
                }
            }), this.orderCouponList(), setTimeout(function() {
                e.setData({
                    stickyRefresh: !1
                });
            }, 1e3);
        },
        isTabActions: function(t) {
            var e = this, o = new Map([ [ i({}, t, 1), function() {
                e.setData({
                    "query.search.is_recommand": 1,
                    "query.search.is_free": 0,
                    "query.search.is_group": 0,
                    "query.search.is_new": 0,
                    "query.search.category_ids": []
                });
            } ], [ i({}, t, 2), function() {
                var t = e.data.datas.list.map(function(t) {
                    return t.id;
                });
                e.setData({
                    "query.search.is_recommand": 0,
                    "query.search.is_free": 0,
                    "query.search.is_group": 0,
                    "query.search.is_new": 0,
                    "query.search.category_ids": t
                });
            } ], [ i({}, t, 3), function() {
                e.setData({
                    "query.search.is_recommand": 0,
                    "query.search.is_free": 1,
                    "query.search.is_group": 0,
                    "query.search.is_new": 0,
                    "query.search.category_ids": []
                });
            } ], [ i({}, t, 4), function() {
                e.setData({
                    "query.search.is_recommand": 0,
                    "query.search.is_free": 0,
                    "query.search.is_group": 1,
                    "query.search.is_new": 0,
                    "query.search.category_ids": []
                });
            } ], [ i({}, t, 5), function() {
                e.setData({
                    "query.search.is_recommand": 0,
                    "query.search.is_free": 0,
                    "query.search.is_group": 0,
                    "query.search.is_new": 1,
                    "query.search.category_ids": []
                });
            } ] ]);
            s(o).filter(function(e) {
                var s = a(e, 2), i = s[0];
                s[1];
                return t === i[t];
            }).forEach(function(t) {
                var s = a(t, 2);
                s[0];
                s[1].call(e);
            });
        },
        onDistanceChange: function(t) {
            var e = t.detail;
            this.setData({
                "query.order_by": e
            }), this.orderCouponList();
        },
        onCityChange: function(t) {
            var e = t.detail;
            this.data.cityId[0].id === Number(e) ? this.setData({
                "query.search.city_id": e,
                "query.search.district_id": 0
            }) : this.setData({
                "query.search.district_id": e
            }), this.orderCouponList();
        },
        onLocation: function() {
            if (this.triggerEvent("onMenuOpen", !0), !this.checkAlbum("scope.userLocation")) return o.tips("获取位置失败，请重新进入后授权");
        },
        onMenuClose: function() {
            this.triggerEvent("onMenuClose", !1);
        },
        onMenuOpen: function() {
            this.triggerEvent("onMenuOpen", !0);
        },
        _initAddressInfo: function() {
            var t = this;
            wx.getStorage({
                key: "provinceData",
                success: function(e) {
                    t._getCityData();
                },
                fail: function(e) {
                    o._getNetWork({
                        url: o.getNetAddresss("member.member-address.address"),
                        success: function(e) {
                            var a = e.data, s = a.data.province, i = a.data.city, o = a.data.district;
                            wx.setStorage({
                                key: "provinceData",
                                data: s
                            }), wx.setStorage({
                                key: "cityData",
                                data: i
                            }), wx.setStorage({
                                key: "districtData",
                                data: o
                            }), t._initAddressInfo();
                        },
                        fail: function(t) {},
                        state: !0
                    });
                }
            });
        },
        _getCityData: function() {
            var t = this;
            wx.getStorage({
                key: "cityData",
                success: function(e) {
                    var a = e.data, s = t.data.cityValue.toString().slice(0, 4), i = a.filter(function(t) {
                        return -1 !== t.id.toString().indexOf(s);
                    }), o = t.data.addressData;
                    o[0].value = i[0].id, t.setData({
                        cityValue: i[0].id,
                        "query.search.city_id": i[0].id,
                        cityId: i,
                        addressData: o
                    }), t._getDistrictData(), t.orderCouponList();
                }
            });
        },
        _getDistrictData: function() {
            var t = this.data.cityId, e = this;
            wx.getStorage({
                key: "districtData",
                success: function(a) {
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t[0].id && s.push(a.data[i]);
                    var o = e.data.addressData;
                    s.forEach(function(t) {
                        o.push({
                            text: t.areaname,
                            value: t.id
                        });
                    }), e.setData({
                        addressData: o
                    });
                }
            });
        },
        checkAlbum: function(a) {
            var s = this;
            return e(t.default.mark(function e() {
                var i;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.next = 2, s.getAuthSetting(a);

                      case 2:
                        return i = t.sent, t.abrupt("return", new Promise(function(t, e) {
                            i ? t(!0) : wx.showModal({
                                title: "请开启授权",
                                content: "是否跳转设置页开启授权",
                                success: function(e) {
                                    e.confirm ? wx.openSetting({
                                        success: function(e) {
                                            e.authSetting[a] ? t(!0) : t(!1);
                                        }
                                    }) : e.cancel && t(!1);
                                },
                                fail: function() {
                                    t(!1);
                                }
                            });
                        }));

                      case 4:
                      case "end":
                        return t.stop();
                    }
                }, e);
            }))();
        },
        getAuthSetting: function(t) {
            return new Promise(function(e, a) {
                wx.getSetting({
                    success: function(a) {
                        a.authSetting[t] ? e(!0) : wx.authorize({
                            scope: t,
                            success: function() {
                                e(!0);
                            },
                            fail: function() {
                                e(!1);
                            }
                        });
                    },
                    fail: function() {
                        e(!1);
                    }
                });
            });
        },
        getMoreData: function() {
            var t = this, e = this;
            if (this.setData({
                isLoadMore: !1
            }), e.data.query.page >= e.data.totalPage) this.data.couponGoodsList.slice(this.data.perPage * this.data.arrIndex).length > 0 && e.setData({
                couponGoodsList: this.data.couponGoodsList.concat(this.data.couponGoodsList.slice(this.data.perPage * this.data.arrIndex, this.data.perPage * (this.data.arrIndex + 1))),
                arrIndex: this.data.arrIndex + 1
            }); else {
                e.setData({
                    "query.page": e.data.query.page + 1
                });
                var a = o.getNetAddresss("plugin.coupon-store.frontend.goods.index"), s = e.data.query;
                o._postNetWork({
                    url: a,
                    data: s,
                    success: function(a) {
                        var s, i, o = a.data, r = o.data.data;
                        1 == o.result ? (e.setData({
                            page: o.data.current_page || t.data.page,
                            couponGoodsList: t.data.couponGoodsList.concat(r),
                            perPage: o.data.per_page,
                            love: null === (s = o.basic_info) || void 0 === s ? void 0 : s.love,
                            point: null === (i = o.basic_info) || void 0 === i ? void 0 : i.point
                        }), e.setData({
                            couponGoodsList: t.data.couponGoodsList.concat(t.data.couponGoodsList.slice(t.data.perPage * t.data.arrIndex, t.data.perPage * (t.data.arrIndex + 1))),
                            arrIndex: t.data.arrIndex + 1
                        }), t.data.couponGoodsList.slice(t.data.perPage * t.data.arrIndex).length > 0 || e.data.page < e.data.totalPage ? e.setData({
                            isLoadMore: !0
                        }) : e.setData({
                            allLoaded: !0
                        })) : (e.setData({
                            page: o.data.current_page || t.data.page,
                            couponGoodsList: t.data.couponGoodsList.concat(o.data.data)
                        }), e.setData({
                            couponGoodsList: data.concat(t.data.couponGoodsList.slice(t.data.perPage * t.data.arrIndex, t.data.perPage * (t.data.arrIndex + 1))),
                            arrIndex: t.data.arrIndex + 1
                        }), t.data.couponGoodsList.slice(t.data.perPage * t.data.arrIndex).length > 0 || e.data.page < e.data.totalPage ? e.setData({
                            isLoadMore: !0
                        }) : e.setData({
                            allLoaded: !0
                        }));
                        e.setData({
                            page: e.data.page - 1,
                            isLoadMore: !1,
                            allLoaded: !0
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            }
        },
        scrollTo: function() {
            var t = this;
            this.createSelectorQuery().select(".consumption-page").boundingClientRect(function(e) {
                e.top > 0 && wx.pageScrollTo({
                    scrollTop: e.top + t.data.pageScrollTopV,
                    duration: 0
                });
            }).exec();
        }
    }
});